#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
//串的实现

//定义串中数据类型
#define MAX_SIZE 100
//定义串是类型
typedef struct{
    char ch[MAX_SIZE];
    int len;
}SeqString;

//给串赋值
void strAssign(SeqString *str, char *chars){
    if(chars == NULL || str == NULL){
        printf("参数错误");
        return;
    }
    int i =0 ;
    while(chars[i] != '\0'){
        str -> ch[i] = chars[i];
        i++;
    }
    str -> ch[i] = '\0';
    str ->len = i;
    printf("%s", str -> ch);
}

//求串长度
int strLength(SeqString *str) {
    if(str == NULL||str->len<=0) {
        return 0;
    }
    return str->len;
}

//复制串
void strCopy(SeqString *des, SeqString *src) {
    if(des == NULL || src == NULL) {
        return;
    }
    if(des->len<0 || src->len >MAX_SIZE) {
        return;
    }
    des ->len = 0;
    for(int i = 0;i<src->len; i++) {
        des->ch[i]=src -> ch[i];
    }
    des->len = src->len;
    des->ch[des->len]='\0';
    printf("\n%s", des -> ch);
}

//判断串是否相等
bool strEqual(SeqString *t1, SeqString *t2) {
    if(t1 == NULL || t2 == NULL) {
        return;
    }
    for(int i = 0;i<t1->len&&i<t2->len;i++) {
        if(t1->ch[i]!=t2->ch[i]){
            return 0;
        }
    }
    return 1;
}

int main(){
    // 设置控制台为UTF-8编码
    #ifdef _WIN32
    system("chcp 65001 > null");
    #endif

    SeqString s;

    //串赋值
    strAssign(&s, "abc");

    //打印串长度
    int len = strLength(&s);
    printf("\n%d", len);

    //复制
    SeqString des;
    strCopy(&des, &s);
    printf("\n%d", des.len);

    //判断是否相等
    SeqString str1,str2;
    strAssign(&str1,"av f");
    strAssign(&str2,"avf");
    if(strEqual(&str1,&str2)){
        printf("\n两个相等");
    } else{
        printf("\n两个不相等");
    }
    return 0;
}